import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/route/route.dart';
import 'package:meal/ui/bar_code.dart';
import 'package:meal/utils/styles.dart';
import 'package:scan/scan.dart';

class WriteOff extends StatefulWidget {
  const WriteOff({Key? key}) : super(key: key);

  @override
  State<WriteOff> createState() => _WriteOffState();
}

class _WriteOffState extends State<WriteOff> {
  ScanController controller = ScanController();

  @override
  void dispose() {
    super.dispose();
    controller.pause();
  }

  @override
  Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
    return Stack(
      alignment: Alignment.center,
      children: [
        ScanView(
          controller: controller,
          scanAreaScale: 0.8,
          scanLineColor: const Color(0xFF00ECD7),
          onCapture: (String data) {
            debugPrint('data: $data');
            controller.resume();
            controller.pause();
            Get.offAndToNamed(RouteGet.groupWriteSuccess);
            // Get.back(result: data);
          },
        ),
        AppBar(
          elevation: 0,
          title: const Text(
            '扫码核销',
            style: TextStyle(fontSize: 20),
          ),
          centerTitle: true,
          backgroundColor: Colors.transparent,
          leading: GestureDetector(
            child: Container(
              color: Colors.transparent,
              child: const Icon(
                Icons.arrow_back_ios,
                color: Colors.white,
                size: 20,
              ),
            ),
            onTap: () {
              Get.back();
            },
          ),
          actions: [
            TextButton(
                onPressed: openAlert,
                child: Text(
                  '手动核销',
                  style: StylesUtils.customTextStyle(fontSize: Sizes.default16),
                ))
          ],
        ),
      ],
    );
  }

  void openAlert() {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (_) {
          return StatefulBuilder(builder: (BuildContext context, setState) {
            return BarCode(callback: (String value) {
              controller.resume();
              controller.pause();
              Get.offAndToNamed(RouteGet.groupWriteSuccess);
            });
          });
        });
  }
}
